package com.micangcaihang.micang.mvp.task.presenter;

import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.micang.entity.ServiceExmaineEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.mvp.task.contract.ExamineServiceContract;
import com.micangcaihang.micang.mvp.task.model.ExamineServiceModel;

import io.reactivex.Observable;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/8/14
 *     update : 2017/8/14
 *     desc   : MiCang
 *     version: 1.0
 * </pre>
 */
public class ExamineServicePresenter extends ExamineServiceContract.Presenter {
    private ExamineServiceModel mModel;

    public void setId(String id) {
        this.id = id;
    }

    private String id;

    @Override
    public void setV(ExamineServiceContract.View v) {
        super.setV(v);
        mModel = new ExamineServiceModel();
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {
        getExmaineServiceData(id);
    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }

    /**
     * 申请终止
     *
     * @param id
     * @return
     */
    @Override
    public void getExmaineServiceData(String id) {
        Observable<ApiResult<ServiceExmaineEntity>> observable = mModel.getExmaineServiceData(id);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<ServiceExmaineEntity>>() {
            @Override
            protected void doOnStart() {
                mView.showLoading();
            }

            @Override
            protected void doOnNext(ApiResult<ServiceExmaineEntity> taskListApiResult) {
                mView.returnExmaineService(taskListApiResult.data);
            }

            @Override
            protected void doOnError(ApiException e) {
                mView.showNetError(e);
            }

            @Override
            protected void doOnComplete() {
                mView.finishRefresh();
            }
        }));
    }

}
